import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Client {

    private String expression;//要计算的式子

    public Client(String expression) {
        this.expression = expression;
        System.out.println(expression);
    }

    public void cal()
    {
        DatagramSocket socket=null;
        try {
            socket = new DatagramSocket();
            InetAddress host = InetAddress.getByName("127.0.0.1");
            int serverPort = 6789;
            byte[] request = expression.getBytes();
            DatagramPacket request_packet = new DatagramPacket(request, request.length,host,serverPort);
            socket.send(request_packet);
            byte[] buffer = new byte[512];
            DatagramPacket reply_packet = new DatagramPacket(buffer, buffer.length);
            socket.receive(reply_packet);
            String result = new String(reply_packet.getData());
            System.out.println("经过计算，结果为:"+result);
        }catch (IOException e){
            System.out.println("IO: " + e.getMessage());
        } finally {
            if (socket != null) socket.close();
        }
    }

    public static void main(String[] args) {
        for(int i=0;i<1000;i++) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入即将要计算的算式：（例如+ 5 6）");
            String expression = scanner.next();
            Client client = new Client(expression);
            client.cal();
        }
    }
}
